package com.ck.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.ck.dao.BanjiDAO;
import com.ck.dao.StudentDAO;
import com.ck.dao.TeacherDAO;
import com.ck.domain.Banji;
import com.ck.domain.Student;
import com.ck.domain.Teacher;
import com.ck.service.TeacherService;

public class TeacherServiceImpl implements TeacherService {
	
	private TeacherDAO teacherDAO;
	private BanjiDAO banjiDAO;
	private StudentDAO studentDAO;
	
	private Student student;

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	public void setBanjiDAO(BanjiDAO banjiDAO) {
		this.banjiDAO = banjiDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Teacher gainTeacher(String username) {
		return teacherDAO.getTeacherByUsername(username);
	}

	public Teacher gainTeacher(Integer id) {
		return teacherDAO.getTeacherById(id);
	}

	public boolean editTeacherOwnInfo(Teacher teacher) {
		teacher.setCst_modify(new Date());
		teacherDAO.updateTeacherOwnInfo(teacher);
		return true;
	}

	public boolean validatePassword(Teacher teacher, String confirmPassword,
			Map model) {
		if(confirmPassword.equals(teacher.getPassword())){
			return true;
		} else {
            model.put("error_confirmPassword", "两次密码输入不一致！");
            return false;
        }
	}
	
	private Teacher md5(Teacher teacher) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        teacher.setPassword(md5.encodePassword(teacher.getPassword(), new String(
                "canyouguessme")));
        return teacher;
    }

    private String md5(String password) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        password = md5.encodePassword(password, new String("canyouguessme"));
        return password;
    }
    
	public boolean editTeacherOwnPassword(String oldPassword, Teacher teacher,
			Map model) {
		Teacher dbTeacher = teacherDAO.getTeacherById(teacher.getId());

		if(dbTeacher.getPassword().equals(md5(oldPassword))){
			md5(teacher);
			teacher.setCst_modify(new Date());
			teacherDAO.updateTeacherOwnPassword(teacher);
			return true;
		}else{
			model.put("error_oldPassword", "原密码不正确！");
            return false;
		}

	}
	
	public List<Banji> gainBanjiList() {
		return banjiDAO.getAllBanji();
	}
	
	public Banji gainBanji(Integer id) {
		return banjiDAO.getBanjiById(id);
	}
	
	public Banji gainBanji(String name) {
		return banjiDAO.getBanjiByName(name);
	}
	
	public boolean addNewBanji(Banji banji, Map model) {
		Banji dbBanji = banjiDAO.getBanjiByName(banji.getName());
		if(dbBanji == null){
			Date now = new Date();
			banji.setCst_create(now);
			banji.setCst_modify(now);
			
			banjiDAO.addNewBanji(banji);
			return true;
		}else {
            model.put("error_name", "该班级名称已经存在！");
            return false;
		}
	}

	public boolean editBanjiInfo(Banji banji, Map model) {
		Banji dbBanji = banjiDAO.getBanjiByName(banji.getName());
		if( dbBanji == null || dbBanji.getId().equals(banji.getId())){
			banji.setCst_modify(new Date());
			banjiDAO.updateBanjiInfo(banji);
			return true;
		}else{
			model.put("error_name", "该班级名称已经存在！");
            return false;
		}
	}

	public List<Student> gainStudentList() {
		return studentDAO.getAllStudent();
	}

	public List<Student> gainStudentByBanji(Integer banji_id) {
		return studentDAO.getStudentByBanji(banji_id);
	}
	
	public boolean addNewStudent(Student student, Map model) {
		Student dbStudent = studentDAO.getStudentByUsername(student.getUsername());
		if(dbStudent == null){
			Date now = new Date();
			student.setCst_create(now);
			student.setCst_modify(now);
			student.setPassword(md5("123456"));
			
			studentDAO.addNewStudent(student);
			return true;
		}else {
            model.put("error_name", "该用户名已经存在！");
            return false;
		}
	}

	public Student gainStudent(Integer id) {
		return studentDAO.getStudentById(id);
	}

	public boolean editStudentInfo(Student student) {
		student.setCst_modify(new Date());
		studentDAO.updateStudentInfo(student);
		return true;
	}

	public void resetStudentPassword(Integer id) {
		student.setId(id);
		student.setCst_modify(new Date());
		student.setPassword(md5("123456"));
		studentDAO.updateStudentPassword(student);
	}


}
